package org.inh.ld20.dangerousbrother;

import java.awt.image.BufferedImage;

public class Animation {
	private BufferedImage[] frames;
	public final double spf;
	private double secondsSinceLastChange;
	private int currentFrame;
	
	public Animation(BufferedImage image) {
		this(new BufferedImage[] {image}, 0);
	}
	
	public Animation(BufferedImage[] frames, double fps) {
		this.frames = frames;
		if(fps <= 0)
			this.spf = -1;
		else
			this.spf = 1.0/fps;
		secondsSinceLastChange = 0;
		currentFrame = 0;
	}
	
	public void tick() {
		if(this.spf < 0.0) return;
		
		secondsSinceLastChange += DangerousBrother.LOGIC_SPF;
		
		while(secondsSinceLastChange >= spf) {
			currentFrame++;
			currentFrame %= frames.length;
			secondsSinceLastChange -= spf;
		}
	}
	
	public BufferedImage getCurrentFrame() {
		return frames[currentFrame];
	}
	
	public int getWidth() {
		return getCurrentFrame().getWidth();
	}
	
	public int getHeight() {
		return getCurrentFrame().getHeight();
	}
}
